﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Data;

namespace Corina.Converters.Win
{
    public class SecondsToReadableStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double totalSeconds = System.Convert.ToDouble(value);
            double days = 0;
            double hours = 0;
            double minutes = 0;
            double seconds = 0;
            if (totalSeconds > 59)
            {
                Common.DatetimeHelper.ConvertSeconds(totalSeconds, out days, out hours, out minutes, out seconds);
            }
            else
            {
                seconds = totalSeconds;
            }
            return string.Format("{0} {1}:{2}:{3}", days, hours, minutes, seconds);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string content = value as string;
            double[] values = Regex.Matches(content, @"\b[0-9]*").OfType<Match>().Select(t => System.Convert.ToDouble(t.Value)).ToArray();

            return (values[0] * 86400) + (values[1] * 3600) + (values[2] * 60) + values[3];
        }
    }
}
